//
//  AppDelegate.swift
//  SSSina
//
//  Created by ss on 2017/6/28.
//  Copyright © 2017年 ss. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
        
        //实例化
        window = UIWindow(frame: UIScreen.main.bounds)
        //背景色
        window?.backgroundColor = UIColor.white
        //设置根控制器
        setupRootViewController()
        //成为主窗口并显示
        window?.makeKeyAndVisible()
        print(SSOAuthViewModel.share.isLogin)
        //注册通知
        NotificationCenter.default.addObserver(self, selector: #selector(switchRootViewController), name:SSSWITCHROOTVIEWCONTROLLER, object: nil)
        return true
    }
    
    //切换根控制器
    @objc private func switchRootViewController(noti:NSNotification){
        
        //判断下object 不为Nil ->ssmainVC
        if let _ = noti.object {
            window?.rootViewController = SSMainViewController()

        }else {
            //如果 object的值是niL则切换到欢迎界面
            window?.rootViewController = SSWelcomeViewController()
        }

    }
    
    //通过用户是否登录来设置根控制器
    private func setupRootViewController(){
        //如果登录了 ->welcome VC
        if SSOAuthViewModel.share.isLogin {
            window?.rootViewController = SSWelcomeViewController()
            
        }else {
            //如果未登录 ->MainVC
            window?.rootViewController = SSMainViewController()
        }
    }
}

